package com.yycx.module.bus.provider.handler.haiying.common;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.module.bus.client.entity.BusDept;
import com.yycx.module.bus.client.entity.BusSaleman;
import com.yycx.module.bus.client.entity.BusSalemanBalance;
import com.yycx.module.bus.client.entity.hy.HyOrder;
import com.yycx.module.bus.provider.service.hy.HyOrderService;
import com.yycx.module.system.client.entity.SysCompany;
import com.yycx.module.system.client.entity.SysOrganization;
import com.yycx.module.user.client.entity.AppPosition;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 业务员选择
 *
 * @author zyf
 */
@Component
public class SaleManSelectPageListHandler implements PageInterceptor<BusSaleman> {

    @Autowired
    private HyOrderService hyOrderService;

    @Override
    public ResultBody validate(CriteriaQuery cq, EntityMap params) {
        return ResultBody.ok();
    }

    @Override
    public void prepare(CriteriaQuery cq, PageParams pageParams, EntityMap params) {
        cq.select(BusSaleman.class, "*");
        cq.addSelect(BusDept.class, "deptName", "deptType");
        cq.like(BusSaleman.class, "saleManName");
        cq.eq(BusDept.class, "deptType");
        cq.eq(BusSaleman.class, "deptId");
        cq.like(BusSaleman.class, "linkTel");
        cq.like(BusSaleman.class, "saleManBusinessNo");
        //只选择有业务编码的人员
        cq.isNotNull("saleman.saleManBusinessNo");
        //职务不为空
        cq.isNotNull("saleman.positionId");
        cq.eq(true, "saleman.saleManType", 1);
        cq.eq(true, "saleman.saleManState", CommonConstants.INT_1);
        cq.createJoin(BusDept.class);
        cq.orderByAsc("saleman.saleManCode");
    }

    @Override
    public List<EntityMap> complete(CriteriaQuery<BusSaleman> cq, List<EntityMap> result, EntityMap extra) {
        String customCode = cq.getParams("customCode");
        Long companyId = OpenHelper.getCompanyId();
        for (EntityMap entityMap : result) {
            Long saleManId = entityMap.getLong("saleManId");
            //查询上一次发货信息收货人
            HyOrder hyOrder = getLastSaleManOrder(saleManId, customCode, companyId);
            if (FlymeUtils.isNotEmpty(hyOrder)) {
                entityMap.put("customUserName", hyOrder.getCustomUserName());
                entityMap.put("customUserTel", hyOrder.getCustomUserTel());
            }
        }
        return PageInterceptor.super.complete(cq, result, extra);
    }

    /**
     * 查询业务员同单位上次发货信息
     *
     * @param saleManId
     * @param customCode
     * @return
     */
    private HyOrder getLastSaleManOrder(Long saleManId, String customCode, Long companyId) {
        CriteriaQuery cq = new CriteriaQuery(HyOrder.class);
        cq.eq(true, "saleManId", saleManId);
        cq.eq(true, "customCode", customCode);
        cq.eq(true, "companyId", companyId);
        cq.orderByDesc("sendDate");
        cq.limit(1);
        return hyOrderService.getOne(cq);
    }

}

